package com.example.shop_2210.cateDetail

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.HistoryEntity
import com.example.base.mvvm.BaseActivity
import com.example.shop_2210.R
import com.example.shop_2210.adapter.GoodsHomeAdapter
import com.example.shop_2210.database.AppDbUtils
import com.example.shop_2210.databinding.ActivityCategoryDetailBinding

@Route(path = "/app/CategoryDetailActivity")
class CategoryDetailActivity : BaseActivity<ActivityCategoryDetailBinding,CategoryDetailViewModel>() {
    @Autowired
    @JvmField
    var cid = 0//分类id
    @Autowired
    @JvmField
    var cname = ""//分类名称
    lateinit var goodsHomeAdapter: GoodsHomeAdapter
    override fun initData() {
        viewModel.success.observe(this){
            if(it.code==200){
                goodsHomeAdapter.data.clear()
                goodsHomeAdapter.data.addAll(it.data.list)
                goodsHomeAdapter.notifyDataSetChanged()
            }
        }
    }

    @SuppressLint("MissingInflatedId")
    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.cname.text = cname
        goodsHomeAdapter = GoodsHomeAdapter()
        binding.rv.adapter = goodsHomeAdapter
        binding.rv.layoutManager = GridLayoutManager(this,2)
        viewModel.search(cid,"",1,10)//先展示对应数据

        //输入内容后，查询
        binding.sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null){//回车后查询
                    viewModel.search(cid,query,1,10)
                    AppDbUtils.getDB().getHistoryDao().insert(HistoryEntity(query))//保存历史搜索记录
                }

                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText!=null){//输入文字后自动查询
                    viewModel.search(cid,newText,1,10)
                }
                return false
            }
        })
        //展示历史搜索记录
        val list = AppDbUtils.getDB().getHistoryDao().query()//获取数据库中保存的历史记录
        if(list.size>0){
            //展示在页面
            binding.cg.removeAllViews()
            list.forEach {
                val keyword = it.keyword
                val view = LayoutInflater.from(this).inflate(R.layout.item_chip, null)
                view.findViewById<TextView>(R.id.chip_text).text = keyword
                binding.cg.addView(view)
                //再次点击view,可以直接执行查询操作
                view.setOnClickListener {
                    binding.sv.setQuery(keyword,true)
                }
            }
        }

    }

    override fun getViewModel(): Class<CategoryDetailViewModel>  = CategoryDetailViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_category_detail

}